package com.example.mydy;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.mydy.model.Announcement;
import com.example.mydy.utils.MockDataManager;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 公告详情页面 - 专为普通用户设计，只能查看公告内容，不能编辑或删除
 */
public class AnnouncementDetailActivity extends AppCompatActivity {
    private static final String TAG = "AnnouncementDetailActivity";

    private TextView tvTitle;
    private TextView tvContent;
    private TextView tvPublishTime;
    private TextView tvPublisher;
    private Button btnBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_announcement_detail);

        initViews();
        loadAnnouncementDetail();
    }

    private void initViews() {
        tvTitle = findViewById(R.id.tv_announcement_title);
        tvContent = findViewById(R.id.tv_announcement_content);
        tvPublishTime = findViewById(R.id.tv_publish_time);
        tvPublisher = findViewById(R.id.tv_publisher);
        btnBack = findViewById(R.id.btn_back);

        btnBack.setOnClickListener(v -> finish());
    }

    private void loadAnnouncementDetail() {
        // 获取传递的公告ID
        int announcementId = getIntent().getIntExtra("announcement_id", -1);
        if (announcementId == -1) {
            Toast.makeText(this, "公告信息加载失败", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 从数据管理器中获取公告详情
        Announcement announcement = MockDataManager.getInstance().getAnnouncementById(announcementId);
        if (announcement == null) {
            Toast.makeText(this, "找不到对应的公告", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 显示公告详情
        tvTitle.setText(announcement.getTitle());
        tvContent.setText(announcement.getContent());
        tvPublisher.setText("发布者: " + announcement.getPublisher());
        
        // 格式化发布时间
        tvPublishTime.setText("发布时间: " + formatDateTime(announcement.getPublishTime()));
    }

    /**
     * 格式化日期时间字符串
     */
    private String formatDateTime(String dateTime) {
        try {
            LocalDateTime localDateTime = LocalDateTime.parse(dateTime);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            return localDateTime.format(formatter);
        } catch (Exception e) {
            Log.e(TAG, "格式化日期时间失败: " + e.getMessage());
            return dateTime; // 如果格式化失败，返回原始字符串
        }
    }
} 